package com.mql.ganio.ui.presenter;

import android.content.DialogInterface;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.cocosw.bottomsheet.BottomSheet;
import com.mql.ganio.MainActivity;
import com.mql.ganio.R;
import com.mql.ganio.api.ApiRetrofit;
import com.mql.ganio.model.response.GanIoResponse;
import com.mql.ganio.ui.adapter.CustomAdapter;
import com.mql.ganio.ui.base.BasePresenter;
import com.mql.ganio.ui.view.IGanHuoAtView;
import com.mql.ganio.util.SPUtils;
import com.mql.ganio.util.UIUtils;

import java.util.ArrayList;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 作者：Administrator on 2017/11/9 17:18
 */
public class CustomAtPresenter extends BasePresenter<IGanHuoAtView> {
    private List<GanIoResponse.ResultsBean> list;
    CustomAdapter mAdapter;
    private MainActivity context;
    private String mType;
    private View mHeaderView;
    int page = 1;
    public static String gank_cala = "gank_cala";

    public CustomAtPresenter(MainActivity context, String mType) {
        super(context);
        list = new ArrayList<>();
        this.context = context;
        this.mType = mType;
    }



    public void refreshData(final int page) {

        mContext.showWaitingDialog(UIUtils.getString(R.string.loading));
        ApiRetrofit.getInstance().getGanIoData(mType, page, 20)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<GanIoResponse>() {
                    @Override
                    public void call(GanIoResponse ganIoResponse) {
                        if (page == 1) {
                            if (mAdapter != null)
                                mAdapter.clear();
                        }
                        List<GanIoResponse.ResultsBean> ganList = ganIoResponse.getResults();
                        list.addAll(ganList);
                        setAdapter(list);
                    }
                });
    }

    private void setAdapter(List<GanIoResponse.ResultsBean> list) {
        if (!mType.equals("Android")) {
            if (mHeaderView == null) {
                mHeaderView = View.inflate(context, R.layout.header_item_gank_custom, null);
                getView().getXRecyclerView().addHeaderView(mHeaderView);
            }
            initHeader(mHeaderView);
        }

        if (mAdapter == null) {
            mAdapter = new CustomAdapter(context, list);
            getView().getXRecyclerView().setLayoutManager(new LinearLayoutManager(context));
            getView().getXRecyclerView().setAdapter(mAdapter);
            boolean isAll = SPUtils.getInstance(context).getString(gank_cala, "全部").equals("全部");
            mAdapter.setAllType(isAll);
        } else {
            mAdapter.setDatas(list);
        }

        getView().getXRecyclerView().refreshComplete();
        getView().getXRecyclerView().loadMoreComplete();
        mContext.hideWaitingDialog();

    }

    private void initHeader(View mHeaderView) {
        final TextView txName = (TextView) mHeaderView.findViewById(R.id.tx_name);
        String gankCala = SPUtils.getInstance(context).getString(gank_cala, "全部");
        txName.setText(gankCala);
        View view = mHeaderView.findViewById(R.id.ll_choose_catalogue);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                new BottomSheet.Builder(context, R.style.BottomSheet_StyleDialog).title("选择分类").sheet(R.menu.gank_bottomsheet).listener(new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                            case R.id.gank_all:
                                if (isOtherType("全部")) {
                                    txName.setText("全部");
                                    mType = "all";// 全部传 all
                                    page = 1;
                                    mAdapter.clear();
                                    SPUtils.getInstance(context).putString(gank_cala, "全部");
                                    refreshData(page);
                                }
                                break;
                            case R.id.gank_ios:
                                if (isOtherType("IOS")) {
                                    txName.setText("IOS");
                                    mType = "iOS";// 这里有严格大小写
                                    page = 1;
                                    mAdapter.clear();
                                    SPUtils.getInstance(context).putString("gank_cala", "IOS");
                                    refreshData(page);
                                }
                                break;
                            case R.id.gank_qian:
                                if (isOtherType("前端")) {
                                    changeContent(txName, "前端");
                                }
                                break;
                            case R.id.gank_app:
                                if (isOtherType("App")) {
                                    changeContent(txName, "App");
                                }
                                break;
                            case R.id.gank_movie:
                                if (isOtherType("休息视频")) {
                                    changeContent(txName, "休息视频");
                                }
                                break;
                            case R.id.gank_resouce:
                                if (isOtherType("拓展资源")) {
                                    changeContent(txName, "拓展资源");
                                }
                                break;
                        }
                    }
                }).show();

            }
        });
    }

    private void changeContent(TextView textView, String content) {
        textView.setText(content);
        mType = content;
        page = 1;
        mAdapter.clear();
        SPUtils.getInstance(context).putString(gank_cala, content);
        refreshData(page);
    }

    private boolean isOtherType(String selectType) {
        String clickText = SPUtils.getInstance(context).getString(gank_cala, "全部");
        if (clickText.equals(selectType)) {
            UIUtils.showToast("当前已经是" + selectType + "分类");
            return false;
        } else {
            // 重置XRecyclerView状态，解决 如出现刷新到底无内容再切换其他类别后，无法上拉加载的情况
            getView().getXRecyclerView().reset();
            return true;
        }
    }
}
